﻿using Microsoft.EntityFrameworkCore;

namespace 多对多
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                /*Student s1 = new Student { Name = "tom" };
                Student s2 = new Student { Name = "lily" };
                Student s3 = new Student { Name = "lucy" };
                Student s4 = new Student { Name = "tim" };
                Student s5 = new Student { Name = "lina" };

                Teacher t1 = new Teacher { Name = "小许" };
                Teacher t2 = new Teacher { Name = "小许1" };
                Teacher t3 = new Teacher { Name = "小许2" };

                t1.Students.Add(s1);
                t1.Students.Add(s2);
                t1.Students.Add(s3);

                t2.Students.Add(s1);
                t2.Students.Add(s3);
                t2.Students.Add(s5);

                t3.Students.Add(s2);
                t3.Students.Add(s4);

                ctx.AddRange(t1, t2, t3);
                ctx.AddRange(s1, s2, s3, s4, s5);
                ctx.SaveChanges();*/

                //Console.WriteLine("老师:" + t1.Id);
                //Console.WriteLine("学生:" + s1.Id);

                //如何在多对多中，给中间表添加其他的东西

                var Teachers = ctx.Teachers.Include(t => t.Students);
                foreach (var item in Teachers)
                {
                    Console.WriteLine("老师:" + item.Name);
                    foreach (var item1 in item.Students)
                    {
                        Console.WriteLine("学生:" + item1.Name);
                    }
                }
            }
        }
    }
}
